package com.cpedu;

import com.cpedu.mapper.BrandeMapper;
import com.cpedu.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
public class MybatisCRUDTest {
    public static void main(String[] args) throws IOException {

    }

    @Test
    public void selectAllTest() throws IOException {
        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        List<Brand> brandList = mapper.selectAllBrand();
        for (Brand brand : brandList) {
            System.out.println(brand);
        }
        sqlSession.close();
    }

    @Test
    public void selectByIdTest() throws IOException {
        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        Brand brand = mapper.selectById(1);
        System.out.println("查询的数据如下: " + brand);
        sqlSession.close();
    }

//    @Test
//    public void selectByIdCondition() throws IOException {
//        定义查询的数据(多条件查询，散装查询)
//        int status = 1;
//        String brandName = "华为";
//        String companyName = "华为";
//        //进行模糊匹配
//        brandName = "%" + brandName + "%";
//        companyName = "%" + companyName + "%";
//        String resource = "spring-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
//        List<Brand> brands = mapper.selectByIdCondition(status, brandName, companyName);
//        System.out.println("散装模糊查询结果为: "+brands);
//        sqlSession.close();
//    }

//    @Test
//    public void selectByIdCondition() throws IOException {
////        定义查询的数据(按实体类查询)
//        int status = 1;
//        String brandName = "华为";
//        String companyName = "华为";
//        //进行模糊匹配
//        brandName = "%" + brandName + "%";
//        companyName = "%" + companyName + "%";
//        //创建实体类对象，进行定义数据
//        Brand brand = new Brand();
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//
//        String resource = "spring-config.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
//        List<Brand> brands = mapper.selectByIdCondition(brand);
//        System.out.println("实体类模糊查询结果为: "+brands);
//        sqlSession.close();
//    }

    @Test
    public void selectByIdCondition() throws IOException {
//        定义查询的数据(按map集合进行查询)
        int status = 1;
        String brandName = "华为";
        String companyName = "华为";
        //进行模糊匹配
        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";
        //创建map对象，进行导入数据
        Map map = new HashMap();
//        map.put("status",status);
        map.put("brandName", brandName);
//        map.put("companyName",companyName);

        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        List<Brand> brands = mapper.selectByIdCondition(map);
        System.out.println("map模糊查询结果为: " + brands);
        sqlSession.close();
    }

    @Test
    public void selectByIdConditionSingle() throws IOException {
        //        定义查询的数据(按map集合进行查询)
        int status = 1;
        String brandName = "华为";
        String companyName = "华为";

        brandName = "%" + brandName + "%";
        companyName = "%" + companyName + "%";
        Brand brand = new Brand();
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        List<Brand> brands = mapper.selectByIdConditionSingle(brand);
        System.out.println(brands);
    }

    @Test
    public void addInsertTest() throws IOException {
        //添加参数
        int status = 1;
        String brandName = "苹果";
        String companyName = "苹果公司";
        int ordered = 100;
        String description = "苹果果";
        //接受参数
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//true表示刷新日志到数据库

        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        mapper.addInsert(brand);
        System.out.println("id= " + brand.getId());

        sqlSession.close();
    }

    //修改(修改一个id里面的所有属性)
    @Test
    public void updateBrandTest() throws IOException {
        //接受参数
        Brand brand = new Brand();
        brand.setId(15);
        brand.setStatus(0);
        brand.setBrandName("小米");
        brand.setCompanyName("小米科技有限公司");
        brand.setDescription("小米yyds1");
        brand.setOrdered(1000);

        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//true表示刷新日志到数据库

        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        int i = mapper.updateBrand(brand);
        System.out.println("id= " + i);

        sqlSession.close();
    }

    //动态修改(修改一个id里面的随意属性数据)
    @Test
    public void updateBrandSingle() throws IOException {
        //接受参数
        Brand brand = new Brand();
        brand.setId(13);
//        brand.setStatus(0);
//        brand.setBrandName("小米");
        brand.setCompanyName("小米科技有限公司");
        brand.setDescription("小米yyds1");
        brand.setOrdered(1000);

        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//true表示刷新日志到数据库

        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        int i = mapper.updateBrandSingle(brand);
        System.out.println("id= " + brand.getId() + "被修改了");

        sqlSession.close();
    }

    //根据id进行删除数据
    @Test
    public void deleteByIdTest() throws IOException {

        Brand brand = new Brand();
        int id = 16;
        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//true表示刷新日志到数据库

        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        mapper.deleteById(id);
        System.out.println("id= " + id + "被删除了...");

        sqlSession.close();
    }


    //删除多项数据
    @Test
    public void deleteByIdsTest() throws IOException {

        Brand brand = new Brand();
        int id[] = {18, 13, 15};
        String resource = "spring-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//true表示刷新日志到数据库

        BrandeMapper mapper = sqlSession.getMapper(BrandeMapper.class);
        mapper.deleteByIds(id);
        System.out.println("数组= " + id + "被删除了...");

        sqlSession.close();
    }
}
